package com.tongzhan.task;

import com.tongzhan.entity.User;
import com.tongzhan.service.BillService;
import com.tongzhan.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 财务上报任务
 */
@Slf4j
@Component
public class BillTaskScheduled {

    @Resource
    private UserService userService;

    @Resource
    private BillService billService;

    /**
     * 每个月10号如未上报财务信息，生成提醒
     */
    @Scheduled(cron = "0 0 0 9 1/1 ?")
    public void billReminder() {
        //获取当前时间
        LocalDate now = LocalDate.now();
        //当前年份
        String year = String.valueOf(now.getYear());
        //当前月份
        String month = String.valueOf(now.getMonthValue());
        //查询该季度上报过财务的宗教团体信息
        List<String> religionCodes = billService.selectReport(year, month, "03");
        //查询未上报宗教团体用户
        List<User> religionUsers = userService.selectUnReport(religionCodes, "03");
        //查询该季度上报过财务的宗教场所信息
        List<String> siteCodes = billService.selectReport(year, month, "04");
        //查询未上报宗教场所用户
        List<User> siteUsers = userService.selectUnReport(siteCodes, "04");
        //查询未上报过财务的民间信仰点
        List<String> codes = billService.selectReport(year, month, "07");
        //查询未上报过财务的民间信仰点
        List<User> users = userService.selectUnReport(codes, "07");
        //查询
        //合并用户列表
        users.addAll(religionUsers);
        users.addAll(siteUsers);
        //角标提醒用户
        userService.signReminder(religionUsers);
    }

//    @Autowired
//    private PersonnelRepository personnelRepository;
//
//    @Autowired
//    private SiteRepository siteRepository;
//
//    @Autowired
//    private StreetRepository streetRepository;
//
//    @Scheduled(cron = "0 0/1 * * * ?")
//    public void test() {
//        List<Personnel> all = personnelRepository.findAll();
//        for (Personnel personnel : all) {
//            String streetName = personnel.getStreetName();
//            Street street = streetRepository.findOne(new Query().addCriteria(Criteria.where("StreetName").is(streetName)));
//            if (street != null) {
//                personnel.setStreetCode(street.getStreetCode());
//                log.info(streetName + "-------------------录入");
//            }
//            String siteName = personnel.getSiteName();
//            Site site = siteRepository.findOne(new Query().addCriteria(Criteria.where("SiteName").is(siteName)));
//            if (site != null) {
//                personnel.setSiteCode(site.getSiteCode());
//                log.info(siteName + "-------------------录入");
//            }
//            personnelRepository.updateByPrimaryKey(personnel);
//        }
//    }
}
